﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace InputProductForm
{
    public partial class frmInputProduct : UTLBASEFORM.MDICHILD
    {
        UTLCOMMAND.UTLCommand command = new UTLCOMMAND.UTLCommand();
        TProductType tProdType = new TProductType();
        TCat tCat = new TCat();
        TProduct tProduct = new TProduct();
        InfoSeed infoSL = new InfoSeed();
        ArrayList arrInfoSL = new ArrayList();
        ArrayList arrInfoSM = new ArrayList();
        InfoSeed infoSM = new InfoSeed();
        InfoShell infoShell = new InfoShell();
        InfoProduct infoPr = new InfoProduct();
        InfoPriceUnit infoPriceUnit = new InfoPriceUnit();
        DataTable dataSeedL;
        DataTable dataSeedS;
        DataTable dataCat;
        DataTable data;

        // id priceUnit
        int idPriceUnit = 0;
        int idSeedM = 0;
        int idSeedL = 0;
        public frmInputProduct()
        {
            InitializeComponent();
        }
        private void frmInputProduct_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            SetTextNumber();
            LoadProductType();
            LoadMaterial();
            LoadUnit();
            LoadQuality();

            LoadProduct();
        }
        private void SetTextNumber()
        {
            NumberTextBox = txtPriceShell;
            NumberTextBox = txtNumSeedL;
            NumberTextBox = txtNumSeedT;
            NumberTextBox = txtPriceSeedT;
            NumberTextBox = txtPriceSeekL;
            NumerTextBoxWithSeparator = txtWSeedT;
            NumerTextBoxWithSeparator = txtWShell;
            NumberTextBox = txtNum;
        }
        private void SetupMenuBar()
        {
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3(TextLabelDelete, ImageNameDelete);
            ShowBTN4("Hủy", null);
            ShowBTN5("In Tem", null);
        }
        public void LoadAutoSeedL()
        {
            string _type = GetTypeSeed(cmbTypeSeedL.Text);
            string _quality = cmdQualityL.SelectedValue.ToString();
            dataSeedL = tProduct.SelectSeed("Hạt Lớn",_type,_quality);
            AutoCompleteStringCollection collec = new AutoCompleteStringCollection();

            for (int i = 0; i < dataSeedL.Rows.Count; i++)
            {
                collec.Add(dataSeedL.Rows[i]["Size"].ToString());
                
            }
            txtWSeedL.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtWSeedL.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtWSeedL.AutoCompleteCustomSource = collec;
        }
        public void LoadAutoSeedS()
        {
            string _type = GetTypeSeed(cmbTypeSeedM.Text);
            string _quality = cmdQualityM.SelectedValue.ToString();
            dataSeedS = tProduct.SelectSeed("Hạt Tấm", _type,_quality);
            AutoCompleteStringCollection collec = new AutoCompleteStringCollection();

            for (int i = 0; i < dataSeedS.Rows.Count; i++)
            {
                collec.Add(dataSeedS.Rows[i]["Size"].ToString());
            }
            txtWSeedT.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtWSeedT.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtWSeedT.AutoCompleteCustomSource = collec;
        }
        public void LoadProductType()
        {
            DataTable data = tProdType.GetProType();
            cmbProductType.DisplayMember = data.Columns["Name"].ToString();
            cmbProductType.ValueMember = data.Columns["ID"].ToString();
            cmbProductType.DataSource = data;

        }
        public void LoadCat(int idType)
        {
            dataCat = tCat.GetCat(idType);
            cmdCat.DataSource = dataCat;
            cmdCat.DisplayMember = dataCat.Columns["Name"].ToString();
            cmdCat.ValueMember = dataCat.Columns["ID"].ToString();
        }

        public void LoadMaterial()
        {
            DataTable data = tProduct.SelectMetarial();
            cmdMaterial.DisplayMember = data.Columns["Name"].ToString();
            cmdMaterial.ValueMember = data.Columns["ID"].ToString();
            cmdMaterial.DataSource = data;
        }
        public void LoadQuality() {
            TQuality t = new TQuality();
            DataTable data = t.GetQuality();
            cmdQualityM.DisplayMember = data.Columns["Name"].ToString();
            cmdQualityM.ValueMember = data.Columns["ID"].ToString();
            cmdQualityM.DataSource = data;
           
            cmdQualityL.DisplayMember = data.Columns["Name"].ToString();
            cmdQualityL.ValueMember = data.Columns["ID"].ToString();
            cmdQualityL.DataSource = data.Copy();
        }
        public void LoadUnit()
        {
            DataTable data = tProduct.SelectUnit();
            cmdUnit.DisplayMember = data.Columns["Name"].ToString();
            cmdUnit.ValueMember = data.Columns["ID"].ToString();
            cmdUnit.DataSource = data;
        }
      
        
        private void cmbProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCat(int.Parse(cmbProductType.SelectedValue.ToString()));
        }
        private void Show(string str)
        {
            //lblshowUntilL.Luong = 1;

        }
        private void SetShowWL()
        {
            string[] tam = txtWSeedL.Text.Trim().Split('.');
            string[] w = new string[] { "", "", "", "" };
            if (tam.Length != 0)
            {
                int num = tam[0].Length;
                if (num > 4) num = 4;
                for (int i = 0; i < num; i++)
                    w[i] = tam[0].Substring(i, 1);
                if (num == 1)
                {
                    showWL.Luong = "";
                    showWL.Chi = "";
                    showWL.Phan = "";
                    showWL.Li = w[0].ToString();
                }
                else if (num == 2)
                {
                    showWL.Luong = "";
                    showWL.Chi = "";
                    showWL.Phan = w[0].ToString();
                    showWL.Li = w[1].ToString();
                }
                else if (num == 3)
                {
                    showWL.Luong = "";
                    showWL.Chi = w[0].ToString();
                    showWL.Phan = w[1].ToString();
                    showWL.Li = w[2].ToString();
                }
                else if (num == 4)
                {

                    showWL.Luong = w[0].ToString();
                    showWL.Chi = w[1].ToString();
                    showWL.Phan = w[2].ToString();
                    showWL.Li = w[3].ToString();
                }


                if (tam.Length > 1)
                    showWL.PhanLe = tam[1].ToString();
            }

        }
        private void txtWSeedL_TextChanged(object sender, EventArgs e)
        {
            SetShowWL();
            int i;
            for (i = 0; i < dataSeedL.Rows.Count; i++)
                if (dataSeedL.Rows[i]["Size"].ToString() == txtWSeedL.Text)
                    break;
            if (i == dataSeedL.Rows.Count)
            {
                txtPriceSeekL.Text = "";
               idSeedL = 0;
            }
            else
            {
                txtPriceSeekL.Text = command.FormatMoney(dataSeedL.Rows[i]["Price"].ToString(), "VDN");
                idSeedL = int.Parse(dataSeedL.Rows[i]["id"].ToString());

            }
        }
        private void SetWM()
        {
            string[] tam = txtWSeedT.Text.Trim().Split('.');
            string[] w = new string[] { "", "", "", "" };
            if (tam.Length != 0)
            {
                int num = tam[0].Length; if (num > 4) num = 4;
                for (int i = 0; i < num; i++)
                    w[i] = tam[0].Substring(i, 1);
                if (num == 1)
                {
                    showWM.Luong = "";
                    showWM.Chi = "";
                    showWM.Phan = "";
                    showWM.Li = w[0].ToString();
                }
                else if (num == 2)
                {
                    showWM.Luong = "";
                    showWM.Chi = "";
                    showWM.Phan = w[0].ToString();
                    showWM.Li = w[1].ToString();
                }
                else if (num == 3)
                {
                    showWM.Luong = "";
                    showWM.Chi = w[0].ToString();
                    showWM.Phan = w[1].ToString();
                    showWM.Li = w[2].ToString();
                }
                else if (num == 4)
                {
                    showWM.Luong = w[0].ToString();
                    showWM.Chi = w[1].ToString();
                    showWM.Phan = w[2].ToString();
                    showWM.Li = w[3].ToString();
                }


                if (tam.Length > 1)
                    showWM.PhanLe = tam[1].ToString();
            }

        }
       
        private void txtWSeedT_TextChanged(object sender, EventArgs e)
        {
            SetWM();
            int i;
            for (i = 0; i < dataSeedS.Rows.Count; i++)
                if (dataSeedS.Rows[i]["Size"].ToString().Trim() == txtWSeedT.Text.Trim())
                    break;
            if (i == dataSeedS.Rows.Count)
            {
                txtPriceSeedT.Text = "";
                idSeedM = 0;
            }
            else
            {
                txtPriceSeedT.Text = command.FormatMoney(dataSeedS.Rows[i]["Price"].ToString(), "VND");
                idSeedM = int.Parse(dataSeedS.Rows[i]["id"].ToString());
            }
        }
        public void GetSeedM()
        {
            infoSM = new InfoSeed();
            infoSM.Id = idSeedM;
            infoSM.Name = "Hạt Tấm";
            infoSM.Size = txtWSeedT.Text;
            if(txtPriceSeedT.Text!="")
            infoSM.Price = float.Parse(command.FormatString(txtPriceSeedT.Text));
            infoSM.DatePrice = DateTime.Parse(dtpkSeedM.Text.ToString());
            infoSM.IDUnit = 0;
            infoSM.Type = GetTypeSeed(cmbTypeSeedM.Text);
            infoSM.Quality = cmdQualityM.SelectedValue.ToString();
            if (txtNumSeedT.Text == "") txtNumSeedT.Text = "1";
            infoSM.Num = int.Parse(txtNumSeedT.Text);
        }
        public void GetSeedL()
        {
            infoSL = new InfoSeed();
            infoSL.Id = idSeedL;
            infoSL.Name = "Hạt Lớn";
            infoSL.Size = txtWSeedL.Text;
            if(txtPriceSeekL.Text!="")
            infoSL.Price = float.Parse(command.FormatString(txtPriceSeekL.Text));

            infoSL.DatePrice = DateTime.Parse(dtpkSeedL.Text.ToString());
            infoSL.IDUnit = 0;
            infoSL.Type = GetTypeSeed(cmbTypeSeedL.Text);
            infoSL.Quality = cmdQualityL.SelectedValue.ToString();
            if (txtNumSeedL.Text == "") txtNumSeedL.Text = "1";
            infoSL.Num = int.Parse(txtNumSeedL.Text);
           
        }
        public void SetArrSeedM()
        {
            GetSeedM();
            //infoSM = new InfoSeed();
            //infoSM.Name = "Hạt Tấm";
            //infoSM.Size = txtWSeedT.Text;
            //infoSM.Price = float.Parse(command.FormatString(txtPriceSeedT.Text));
            //infoSM.DatePrice = DateTime.Parse(dtpkSeedM.Text.ToString());
            //infoSM.IDUnit = 0;
            //infoSM.Type = GetTypeSeed(cmbTypeSeedM.Text);
            //infoSM.Quality = cmdQualityM.Text;
            //infoSM.Num = int.Parse(txtNumSeedT.Text);
            arrInfoSM.Add(infoSM);
        }
        public void SetArrSeedL()
        {
            GetSeedL();
            //infoSL = new InfoSeed();
            //infoSL.Name = "Hạt Lớn";
            //infoSL.Size = txtWSeedL.Text;
            //infoSL.Price = float.Parse(command.FormatString(txtPriceSeekL.Text));
            //infoSL.DatePrice = DateTime.Parse(dtpkSeedL.Text.ToString());
            //infoSL.IDUnit = 0;
            //infoSL.Type = GetTypeSeed(cmbTypeSeedL.Text);
            //infoSL.Quality = cmdQualityL.Text;
            //infoSL.Num = int.Parse(txtNumSeedL.Text);
            arrInfoSL.Add(infoSL);
        }
        public void GetShell()
        {
            if (int.Parse(cmdUnit.SelectedValue.ToString()) == 1)
                infoShell.Weight = int.Parse(txtWShell.Text);
            else { 
                infoShell.Weight = double.Parse(ctrWShell.Luong + ctrWShell.Chi+ctrWShell.Phan+ctrWShell.Li); }

            infoShell.Price = float.Parse(command.FormatString(txtPriceShell.Text));
            infoShell.IDMaterial = 1; //int.Parse(cmdMaterial.SelectedValue.ToString());
            infoShell.UnitID = int.Parse(cmdUnit.SelectedValue.ToString());
            infoShell.Change = cbChange.Checked;
            if (cbChange.Checked == false) infoShell.Price = 0;
            infoShell.IdPriceUnit = infoPriceUnit.ID;
            infoShell.GoldType = cmdMaterial.Text;
            infoShell.Source = txtSource.Text;
        }
        public void GetProduct()
        {
            infoPr.Name = cmdCat.Text;
            infoPr.IDCat = int.Parse(cmdCat.SelectedValue.ToString());
            infoPr.IDSeedLarge = 0;
            infoPr.NumberSeedLarge = int.Parse(txtNumSeedL.Text);
            infoPr.IDSeedSmall = 0;
            infoPr.NumberSeedSmall = int.Parse(txtNumSeedT.Text);
            infoPr.IDShell = infoShell.ID;
            if(txtTienCong.Text!="")
            infoPr.TienCong = float.Parse(command.FormatString(txtTienCong.Text));
            infoPr.Statue = 0;
            infoPr.CreateDate = DateTime.Now;
            infoPr.Barcode = SetBacode();
        }
        public string SetBacode()
        {
            string barcode = "";
            string code = tProduct.GetMaxID(int.Parse(cmdCat.SelectedValue.ToString()));
            if (code == DBNull.Value.ToString()) code = "0";
            int i;
            for (i = 0; i < dataCat.Rows.Count; i++)
                if (dataCat.Rows[i]["ID"].ToString() == cmdCat.SelectedValue.ToString())
                    break;
            string str = dataCat.Rows[i]["Symbol"].ToString();

            code = (int.Parse(code) + 1).ToString("00000000");
            if (str.Trim().Length == 1) str = str.Trim() + "0";
            barcode = str.Trim() + code;
            return barcode;
        }
        public void SetPriceUnit() {

            infoPriceUnit.IdType =int.Parse(cmbProductType.SelectedValue.ToString());
            infoPriceUnit.IdUnit =int.Parse(cmdUnit.SelectedValue.ToString());
            infoPriceUnit.IdMaterial = int.Parse(cmdMaterial.SelectedValue.ToString());
            if (txtPriceShell.Text != "")
                infoPriceUnit.Price = float.Parse(command.FormatString(txtPriceShell.Text));
            else infoPriceUnit.Price = 0;
           infoPriceUnit.Date = DateTime.Now;
        }
        public void InsertArrSeed() {
            for (int i = 0; i < arrInfoSL.Count; i++) {
              int t= InsertSe((InfoSeed) arrInfoSL[i]);
              ((InfoSeed) arrInfoSL[i]).Id = t;
            }
            for (int i = 0; i < arrInfoSM.Count; i++)
            {
                int t = InsertSe((InfoSeed)arrInfoSM[i]);
                ((InfoSeed)arrInfoSM[i]).Id = t;
            }
        }
        public int InsertSe(InfoSeed info)
        {
            if (info.Id == 0)
                info.Id = tProduct.InsertSeed(info);
            else
            {
                tProduct.UpdateSeed(info);
            }
            return info.Id;
        }
        public int InsertSh()
        {
            infoShell.ID = tProduct.InsertShell(infoShell);
            return infoShell.ID;
        }
        public int InsertPr()
        {
            infoPr.ID = tProduct.InsertProduct(infoPr);
            return infoPr.ID;
        }      
        public void InsertPr_Seed() {
            for(int i=0;i< arrInfoSM.Count;i++)
                tProduct.InsertProductSeed(infoPr.ID, ((InfoSeed)arrInfoSM[i]).Id, ((InfoSeed)arrInfoSM[i]).Num);
            for (int i = 0; i < arrInfoSL.Count; i++)
                tProduct.InsertProductSeed(infoPr.ID, ((InfoSeed)arrInfoSL[i]).Id,((InfoSeed)arrInfoSL[i]).Num);


        }
        public void LoadProduct()
        {
            lvProduct.Items.Clear();
            data = tProduct.LoadProductWithStatus(0);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());

                lvProduct.Items.Add(item);

                // item.SubItems.Add(stt.ToString());

                item.SubItems.Add(data.Rows[i]["Name"].ToString());
                item.SubItems.Add(data.Rows[i]["Barcode"].ToString());
                item.SubItems.Add(data.Rows[i]["Weight"].ToString());

                item.SubItems.Add(data.Rows[i]["GoldType"].ToString());
                item.SubItems.Add(data.Rows[i]["NameUnit"].ToString().Trim());
                item.SubItems.Add(data.Rows[i]["CreateDate"].ToString());
               
                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }
        }
        public override void btn1_Click(object sender, EventArgs e)
        {
           // GetSeed();
           
                InsertArrSeed();
                //InsertSe(infoSL);
                // InsertSe(infoSM);
                if (cbChange.Checked == false)
                {
                    SetPriceUnit();
                    InsertPriceUnit(infoPriceUnit);
                }
            for (int i = 0; i < int.Parse(txtNum.Text); i++)
            {
                GetShell();
                InsertSh();
                GetProduct();
                InsertPr();
                InsertPr_Seed();
            }
            LoadProduct();
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            base.btn2_Click(sender, e);
        }
        public override void btn3_Click(object sender, EventArgs e)
        {
            base.btn3_Click(sender, e);
        }
        
        private void SetWShell( string str)
        {
            string[] tam = str.Trim().Split('.');
            string[] w = new string[] { "", "", "", "" };
            if (tam.Length != 0)
            {
                int num = tam[0].Length; if (num > 4) num = 4;
                for (int i = 0; i < num; i++)
                    w[i] = tam[0].Substring(i, 1);
                if (num == 1)
                {
                    ctrWShell.Luong = "";
                    ctrWShell.Chi = "";
                    ctrWShell.Phan = "";
                    ctrWShell.Li = w[0].ToString();
                }
                else if (num == 2)
                {
                    ctrWShell.Luong = "";
                    ctrWShell.Chi = "";
                    ctrWShell.Phan = w[0].ToString();
                    ctrWShell.Li = w[1].ToString();
                }
                else if (num == 3)
                {
                    ctrWShell.Luong = "";
                    ctrWShell.Chi = w[0].ToString();
                    ctrWShell.Phan = w[1].ToString();
                    ctrWShell.Li = w[2].ToString();
                }
                else if (num == 4)
                {
                    ctrWShell.Luong = w[0].ToString();
                    ctrWShell.Chi = w[1].ToString();
                    ctrWShell.Phan = w[2].ToString();
                    ctrWShell.Li = w[3].ToString();
                }

            }

                if (tam.Length > 1)
                    ctrWShell.PhanLe = tam[1].ToString();
        }

        public void ShowListToFrom(int i) {
            DataTable dataSeed = tProduct.SelectSeedByProduct(int.Parse(data.Rows[i]["ID"].ToString()));
            arrInfoSL.Clear();
            arrInfoSM.Clear();
            lvSeedL.Items.Clear();
            lvSeedM.Items.Clear();
            for (int j = 0; j < dataSeed.Rows.Count; j++)
            {
                if (dataSeed.Rows[j]["Name"].ToString().Trim() == "Hạt Tấm")
                {
                    infoSM = new InfoSeed();
                    infoSM.Id = int.Parse(dataSeed.Rows[j]["id"].ToString());
                    infoSM.Name = "Hạt Tấm";
                    infoSM.Size = dataSeed.Rows[j]["Size"].ToString();
                    infoSM.Price = float.Parse(dataSeed.Rows[j]["Price"].ToString());
                    infoSM.DatePrice = DateTime.Parse(dataSeed.Rows[j]["DatePrice"].ToString());
                    infoSM.IDUnit = 0;
                    infoSM.Type = dataSeed.Rows[j]["Type"].ToString();
                    infoSM.Quality = dataSeed.Rows[j]["Quality"].ToString();
                    infoSM.Num = int.Parse(dataSeed.Rows[j]["Num"].ToString());

                    arrInfoSM.Add(infoSM);
                }
                else
                {
                    infoSL = new InfoSeed();
                    infoSL.Id = int.Parse(dataSeed.Rows[j]["id"].ToString());
                    infoSL.Name = "Hạt Lớn";
                    infoSL.Size = dataSeed.Rows[j]["Size"].ToString();
                    infoSL.Price = float.Parse(dataSeed.Rows[j]["Price"].ToString());
                    infoSL.DatePrice = DateTime.Parse(dataSeed.Rows[j]["DatePrice"].ToString());
                    infoSL.IDUnit = 0;
                    infoSL.Type = dataSeed.Rows[j]["Type"].ToString();
                    infoSL.Quality = dataSeed.Rows[j]["Quality"].ToString();
                    infoSL.Num = int.Parse(dataSeed.Rows[j]["Num"].ToString());
                    arrInfoSL.Add(infoSL);
                }
            }
            LoadList(arrInfoSM, lvSeedM);
            LoadList(arrInfoSL, lvSeedL);
            cmbProductType.Text = data.Rows[i]["NameType"].ToString();
            cmdCat.Text = data.Rows[i]["Name"].ToString();
            cmdUnit.Text = data.Rows[i]["NameUnit"].ToString();
            if (int.Parse(cmdUnit.SelectedValue.ToString()) == 1)
                txtWShell.Text = data.Rows[i]["Weight"].ToString();
            else if (int.Parse(cmdUnit.SelectedValue.ToString()) == 28)
            {
               SetWShell(data.Rows[i]["Weight"].ToString());
            }
            cbChange.Checked = bool.Parse( data.Rows[i]["Change"].ToString());
            txtPriceShell.Text = command.FormatMoney(data.Rows[i]["Price"].ToString(), "VND");
            cmdMaterial.Text = data.Rows[i]["GoldType"].ToString();
            txtSource.Text =data.Rows[i]["Source"].ToString();
            txtTienCong.Text = command.FormatMoney(data.Rows[i]["TienCong"].ToString(),"VND");
           // dtpkSeedM.Text = data.Rows[i]["dateM"].ToString();
        
        }

        private void lvProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lv = (ListView)sender;

            if (lv.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (lv.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {
                    ShowListToFrom(flag);
                }
            }

        }


        private void txtPriceShell_Leave(object sender, EventArgs e)
        {

            txtPriceShell.Text = command.FormatMoney(txtPriceShell.Text, "VND");
        }
        private void txtTienCong_Leave(object sender, EventArgs e)
        {
            txtTienCong.Text = command.FormatMoney(txtTienCong.Text, "VND");
        }
        private void txtPriceSeekL_Leave(object sender, EventArgs e)
        {
            txtPriceSeekL.Text = command.FormatMoney(txtPriceSeekL.Text, "VND");

        }
        private void txtPriceSeedT_Leave(object sender, EventArgs e)
        {
            txtPriceSeedT.Text = command.FormatMoney(txtPriceSeedT.Text, "VND");
        }
        private void cmdUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(cmdUnit.SelectedValue.ToString()) == 1)
            {
                txtWShell.Visible = true;
                ctrWShell.Visible = false;
            }
            else
            {
                txtWShell.Visible = false;
                ctrWShell.Visible = true;
            }
        }
        private void cbChange_CheckedChanged(object sender, EventArgs e)
        {

            DataTable dataPriceUnit;
            if (cbChange.Checked == false)
            {
                lblGiaShell.Text = "Đơn Giá:";
                dataPriceUnit = tProduct.SelectPriceUnit(int.Parse(cmbProductType.SelectedValue.ToString()), int.Parse(cmdUnit.SelectedValue.ToString()), int.Parse(cmdMaterial.SelectedValue.ToString()));
                if (dataPriceUnit.Rows.Count <= 0)
                    infoPriceUnit.ID = 0;
                if (dataPriceUnit.Rows.Count > 0)
                {
                    idPriceUnit = int.Parse(dataPriceUnit.Rows[0]["ID"].ToString());
                    txtPriceShell.Text = dataPriceUnit.Rows[0]["Price"].ToString() + "VND/" + cmdUnit.Text;
                    infoPriceUnit.ID = idPriceUnit;
                }
            }
            else if (cbChange.Checked == true)
            {
                lblGiaShell.Text = "Giá:";
                idPriceUnit = 0;
                txtPriceShell.Text = "";
            }

        }

        private int InsertPriceUnit( InfoPriceUnit info) {
            if (info.ID == 0)
                info.ID = tProduct.InsertPriceUnit(info);
            else
            {
                tProduct.UpdatePriceUnit(info);
            }
            return info.ID;
        }
        public void LoadList(ArrayList arr , ListView lv ) {
            lv.Items.Clear();
            string size="";
            foreach (InfoSeed a in arr)
            {
                size = FormatWeight(a.Size.ToString());
                ListViewItem item = new ListViewItem(a.Type + ":" + size);
                lv.Items.Add(item);
                // item.SubItems.Add(stt.ToString());
                item.SubItems.Add(a.Num.ToString());
            }
        }
        public string FormatWeight(string str) {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    size = tam[0]+ "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        } 
        public string GetTypeSeed(string str)
        {
            string[] temp = str.Split('(');
            string[] temp2 = temp[temp.Length-1].Split(')');
            return temp2[0];
        }
        private void cmdAddSeedM_Click(object sender, EventArgs e)
        {
            SetArrSeedM();
            LoadList(arrInfoSM, lvSeedM);
        }
        private void cmdAddSeedL_Click(object sender, EventArgs e)
        {
            SetArrSeedL();
          
            LoadList(arrInfoSL, lvSeedL);
        }
        private void cmDelM_Click(object sender, EventArgs e)
        {
            int flag = -1;
            for (int i = 0; i < lvSeedM.Items.Count; i++)
            {
                if (lvSeedM.Items[i].Selected)
                {
                    flag = i;
                }
            }
            if (flag != -1)
            {

                arrInfoSM.RemoveAt(flag);
                LoadList(arrInfoSM, lvSeedM);
            }
        }
        private void cmDelL_Click(object sender, EventArgs e)
        {
            int flag = -1;
            for (int i = 0; i < lvSeedL.Items.Count; i++)
            {
                if (lvSeedL.Items[i].Selected)
                {
                    flag = i;
                }
            }
            if (flag != -1)
            {

                arrInfoSL.RemoveAt(flag);
                LoadList(arrInfoSL, lvSeedL);
            }
        }

        private void cmdQualityM_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadAutoSeedS();
        }

        private void cmdQualityL_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadAutoSeedL();
        }

        private void ShowSeedL( int i) {
            cmbTypeSeedL.Text = ((InfoSeed)arrInfoSL[i]).Type;
            cmdQualityL.SelectedValue = ((InfoSeed)arrInfoSL[i]).Quality;
            txtWSeedL.Text = ((InfoSeed)arrInfoSL[i]).Size;
            txtPriceSeekL.Text = command.FormatMoney(((InfoSeed)arrInfoSL[i]).Price.ToString(), "VND");
            txtNumSeedL.Text = ((InfoSeed)arrInfoSL[i]).Num.ToString();
            dtpkSeedL.Text =((InfoSeed)arrInfoSL[i]).DatePrice.ToString();
        }
        private void ShowSeedM(int i)
        {
            cmbTypeSeedM.Text = ((InfoSeed)arrInfoSM[i]).Type;
            cmdQualityM.SelectedValue = ((InfoSeed)arrInfoSM[i]).Quality;
            txtWSeedT.Text = ((InfoSeed)arrInfoSM[i]).Size;
            txtPriceSeedT.Text = command.FormatMoney(((InfoSeed)arrInfoSM[i]).Price.ToString(), "VND");
            txtNumSeedT.Text = ((InfoSeed)arrInfoSM[i]).Num.ToString();
            dtpkSeedM.Text =((InfoSeed)arrInfoSM[i]).DatePrice.ToString();
        }
        private void lvSeedM_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lv = (ListView)sender;

            if (lv.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (lv.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {
                    ShowSeedM(flag);
                }
            }
        }

        private void lvSeedL_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lv = (ListView)sender;

            if (lv.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (lv.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {
                    ShowSeedL(flag);
                }
            }
        }

        private void cmdAddTypePro_Click(object sender, EventArgs e)
        {
            frmAddMat frm = new frmAddMat();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadProductType();
        }

        private void cmdAddCat_Click(object sender, EventArgs e)
        {
            frmAddCat frm = new frmAddCat();
            frm.FormClosed +=new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        private void cmdAddQuality_Click(object sender, EventArgs e)
        {
            frmAddQuality frm = new frmAddQuality();
            frm.FormClosed +=new FormClosedEventHandler(frmQu_FormClosed);
            frm.ShowDialog();
        }
        void frmQu_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadQuality();
        }
        private void cmbAddMaterial_Click(object sender, EventArgs e)
        {
            frmAddMaterial frm = new frmAddMaterial();
           
            frm.FormClosed += new FormClosedEventHandler(frmMateria_FormClosed);   
            frm.ShowDialog();
        }

        void frmMateria_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadMaterial();
        }

    }
}